<?php
ifi!$_GET['cmd']) {
	//generating list of categories
	if(!perms_check('categories', 'edit') and !perms_check('categories', 'del')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	$content = string_template(read_file('admin/themes/categories_header.php'), array('theme_path'=>$GLOBALS['theme_path']));
	list_categories(0, 0, 'admin', 1, $content, 0);
	$content .= string_template(read_file('admin/themes/categories_footer.php'), array('theme_path'=>$GLOBALS['theme_path']));
}
elseif($_GET['cmd'] == 'add') {
	if(!perms_check('categories', 'add')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	$categories = NULL;
	list_categories(0, 0, 'select', 1, $categories, $_GET['cat']);

	$array = array('function' => 'cat_add', 'id' => '', 'parent' => 1, 'categories' => $categories);

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/categories_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$t = "name_$l";
		$array["c_$t"] = '';
		$t = "desc_$l";
		$array["c_$t"] = '';

		//recover
		if(ereg('module=error', $_SERVER['HTTP_REFERER'])) {
			$t = "name_$l";
			$m_array["c_$t"] = $_SESSION['recover_'.$t];
			$t = "desc_$l";
			$m_array["c_$t"] = $_SESSION['recover_'.$t];   
		}
		//	
	}

	$content = string_template(read_file('admin/themes/categories_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd'] == 'del') {
	if(!perms_check('categories', 'del')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}
	else {
		SQLvalidate($_GET['id']);

		$db = new dbquery;

		$db->query("SELECT * FROM $conf[prefix]categories WHERE id='$_GET[id]'") or $db->err(__FILE__, __LINE__);
		$d = $db->fetch_array();

		$yes = '<input type="button" onClick="javascript:location.href=\'index.php?module=categories&function=cat_delete&id='.$_GET['id'].'&amp;k='.$_GET['id'].'\'" value="'.$lang['yes'].'" class="button">';
		$no='<input type="button" onClick="javascript:history.go(-1);" value="'.$lang['no'].'" class="button">';
		$content = string_template($lang['admin_really_delete'], array('yes' => $yes, 'no' => $no, 'what' => $d["name_$_SESSION[lang_short]"]));
	}
}
elseif($_GET['cmd'] == 'edit') {
	if(!perms_check('categories', 'edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	SQLvalidate($_GET['id']);

	$db = new dbquery;

	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $_GET[id]") or $db->err(__FILE__, __LINE__);

	$cat = $db->fetch_object();

	$categories = NULL;
	list_categories(0, 0, $cat->parent, 1, $categories, $cat->id);

	$array = array('function' => 'cat_edit', 'id' => $_GET[id], 'parent' => $cat->parent, 'categories' => $categories);

	//multi_lang
	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/categories_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$t = "name_$l";
		$array["c_$t"] = read_text_rest($cat->$t);
		$t = "desc_$l";
		$array["c_$t"] = read_text_edit($cat->$t);
	}

	$content = string_template(read_file('admin/themes/categories_form.php'), array('multi' => $multi));
	//

	$content = string_template($content, $array);
}

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['admin_categories']));
//

?>
